﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Binaria.Core.Web;
using Com.Binaria.SISCOB.Funcional.DTO;

namespace Com.Binaria.SISCOB.Web.UI 
{

    public partial class ucConciliacion : System.Web.UI.UserControl, IView<ConciliacionFuncionalDTO>
    {
        #region Miembros de IView<ConciliacionFuncionalDTO>

        public event ViewAddHandler OnAdd;
        public event ViewSaveHandler OnSave;

        public string Message
        {
            set { this.lblMessage.Text = value; }
        }

        public IList<ConciliacionFuncionalDTO> Items
        {
            set { }
        }

        public ConciliacionFuncionalDTO Item
        {
            set
            {
                if (value == null) return;
                //Loop por cada una de las propiedades
                ctlId.Value = value.Id.ToString();
                ctlCuentaCorrienteE.SelectedValue = value.CuentaCorriente.ToString();
                ctlSaldoMayorE.Text = value.SaldoMayor.ToString();
                ctlSaldoCuentaE.Text = value.SaldoCuenta.ToString();
                ctlFchDesdeTextE.Text = value.FchDesdeText;
                ctlFchHastaTextE.Text = value.FchHastaText;
                ctlFchCreacionTextE.Text = value.FchCreacionText;
                ctlObsE.Text = value.Obs;
                ctlEstaCerradaE.Checked = value.EstaCerrada;
                ctlUserNameE.Text = value.UserName;

                //Relacion [MD18]: ItemConcepto
                //if (value.ItemConcepto != null)
                //{
                //   grdItemConcepto.DataSource = value.ItemConcepto;
                //   grdItemConcepto.DataBind();
                //}
            }
            get
            {
                ConciliacionFuncionalDTO oItem = new ConciliacionFuncionalDTO();
                if (!string.IsNullOrEmpty(ctlId.Value)) oItem.Id = Convert.ToInt32(ctlId.Value);
                oItem.CuentaCorriente = Convert.ToInt32(ctlCuentaCorrienteE.SelectedValue);
                if (!string.IsNullOrEmpty(ctlSaldoMayorE.Text)) oItem.SaldoMayor = Convert.ToInt64(ctlSaldoMayorE.Text.Replace(".",""));
                if (!string.IsNullOrEmpty(ctlSaldoCuentaE.Text)) oItem.SaldoCuenta = Convert.ToInt64(ctlSaldoCuentaE.Text.Replace(".", ""));
                if (!string.IsNullOrEmpty(ctlFchDesdeTextE.Text)) oItem.FchDesdeText = ctlFchDesdeTextE.Text;
                if (!string.IsNullOrEmpty(ctlFchHastaTextE.Text)) oItem.FchHastaText = ctlFchHastaTextE.Text;
                if (!string.IsNullOrEmpty(ctlFchCreacionTextE.Text)) oItem.FchCreacionText = ctlFchCreacionTextE.Text;
                if (!string.IsNullOrEmpty(ctlObsE.Text)) oItem.Obs = ctlObsE.Text;
                oItem.EstaCerrada = ctlEstaCerradaE.Checked;
                if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) oItem.UserName = HttpContext.Current.User.Identity.Name;

                //Relacion [MD18]: ItemConcepto
                //oItem.ItemConcepto = (IList<ItemConceptoFuncionalDTO>)grdItemConcepto.DataSource;
                return oItem;
            }
        }

        IRelacion[] _Relaciones = null;
        public IRelacion[] Relaciones
        {
            get
            {
                if (_Relaciones == null)
                {
                    _Relaciones = new RelacionItem[2];
                    _Relaciones[0] = new RelacionItem() { IdentityName = "CuentaCorriente", Target = new string[] {"CuentaCorriente"} };
                    _Relaciones[1] = new RelacionItem() { IdentityName = "DocumentoTransaccion", Target = new string[] {"DocumentoTransaccionKyAc18ItemConcepto"} };
                }
                return _Relaciones;
            }
        }

        public void ChangeMode(TypeAction Action)
        {
            pnlFind.Visible = false;
            pnlEdit.Visible = false;
            pnlGrid.Visible = false;
            //
            switch (Action)
            {
                case TypeAction.ALL:
                case TypeAction.FND:
                    pnlFind.Visible = true;
                    pnlGrid.Visible = true;
                    break;
                case TypeAction.EDI:
                case TypeAction.ADD:
                    pnlEdit.Visible = true;
                    //fakeData(grdItemConcepto, new List<ItemConceptoFuncionalDTO>(new ItemConceptoFuncionalDTO[1] {new ItemConceptoFuncionalDTO() }));
                    break;
            }
        }

        public void DataBindRelaciones(TypeAction Action)
        {
            if (Relaciones == null) return;
            for (int i = 0; i < Relaciones.Length; i++)
            {
               for (int t = 0; t < Relaciones[i].Target.Length; t++)
               {
                    DropDownList oCbo = FindControl(string.Format("ctl{0}{1}"
                                                                , Relaciones[i].Target[t]
                                                                , (Action == TypeAction.ALL) ? "" : "E")) as DropDownList;
                    if (oCbo != null)
                    {
                        oCbo.DataSource = Relaciones[i].DataSource;
                        oCbo.DataBind();
                    }
               }
            }
        }
        #endregion

        #region EVENTOS PROPIOS DEL CONTROL
        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            if (OnSave != null && OnSave(false))
            {
                Page.ClientScript.RegisterStartupScript(typeof(ucConciliacion), "closeBox", "if (typeof(self.parent.$.Mantto) != null) {self.parent.$.Mantto.ActionEditClose('Informaci&oacute;n registrada satisfactoriamente');}", true);
            }
        }

        void fakeData(GridView oGrid, IList oList)
        {
           if (oGrid.DataSource == null || ((IList)oGrid.DataSource).Count < 1)
            {
                oGrid.DataSource = oList;
                oGrid.DataBind();
                // Get the total number of columns in the GridView to know what the Column Span should be
                int columnsCount = oGrid.Columns.Count;
                oGrid.Rows[0].Cells.Clear();// clear all the cells in the row
                oGrid.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                oGrid.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
                //You can set the styles here
                oGrid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                oGrid.Rows[0].Cells[0].Font.Bold = true;
                //set No Results found to the new added cell
                oGrid.Rows[0].Cells[0].Text = oGrid.EmptyDataText;
            }
        }


        protected void grdItemConcepto_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView oGrid = sender as GridView;
            if (e.RowIndex >= 0)
            {
                IList oLst = (IList)oGrid.DataSource;
                if (oLst != null && oLst.Count > 0)
                {
                    oLst.RemoveAt(e.RowIndex); //Fila indicada
                    if (oLst.Count > 0) oGrid.DataBind();
                    else fakeData(oGrid, new List<ItemConceptoFuncionalDTO>(new ItemConceptoFuncionalDTO[1] {new ItemConceptoFuncionalDTO() }));
                }
            }
        }
        
        protected void grdItemConcepto_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ItemConceptoFuncionalDTO oFDTO = (ItemConceptoFuncionalDTO)e.Row.DataItem;
                if (oFDTO == null) return;
                //
                switch (e.Row.RowState)
                {
                    case DataControlRowState.Normal:
                    case DataControlRowState.Alternate:
                        DropDownList ddlDocumentoTransaccionKyAc18ItemConcepto = (DropDownList)e.Row.FindControl("ctlDocumentoTransaccionKyAc18ItemConcepto");
                        if (ddlDocumentoTransaccionKyAc18ItemConcepto != null)
                        {
                            if (ddlDocumentoTransaccionKyAc18ItemConcepto.Items.Count < 1)
                                SetRelacion("DocumentoTransaccion", ddlDocumentoTransaccionKyAc18ItemConcepto);

                            ddlDocumentoTransaccionKyAc18ItemConcepto.SelectedValue = oFDTO.DocumentoTransaccion.ToString();
                        }
                        break;
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                SetRelacion("DocumentoTransaccion", (DropDownList)e.Row.FindControl("ctlDocumentoTransaccionKyAc18ItemConcepto"));
            }
        }
        
        protected void grdItemConcepto_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView oGrid = sender as GridView;
            //
            if (!string.IsNullOrEmpty(e.CommandName))
            {
                DropDownList ctlDocumentoTransaccionKyAc18ItemConcepto = (DropDownList)oGrid.FooterRow.FindControl("ctlDocumentoTransaccionKyAc18ItemConcepto");
                ctlDocumentoTransaccionKyAc18ItemConcepto.SelectedIndex = -1;

                TextBox ctlNmrDocumentoKyAc18ItemConcepto = (TextBox)oGrid.FooterRow.FindControl("ctlNmrDocumentoKyAc18ItemConcepto");
                ctlNmrDocumentoKyAc18ItemConcepto.Text = string.Empty;

                TextBox ctlMontoKyAc18ItemConcepto = (TextBox)oGrid.FooterRow.FindControl("ctlMontoKyAc18ItemConcepto");
                ctlMontoKyAc18ItemConcepto.Text = string.Empty;

                TextBox ctlFchDocumentoTextKyAc18ItemConcepto = (TextBox)oGrid.FooterRow.FindControl("ctlFchDocumentoTextKyAc18ItemConcepto");
                ctlFchDocumentoTextKyAc18ItemConcepto.Text = string.Empty;

                TextBox ctlTipoKyAc18ItemConcepto = (TextBox)oGrid.FooterRow.FindControl("ctlTipoKyAc18ItemConcepto");
                ctlTipoKyAc18ItemConcepto.Text = string.Empty;

                TextBox ctlGlosaKyAc18ItemConcepto = (TextBox)oGrid.FooterRow.FindControl("ctlGlosaKyAc18ItemConcepto");
                ctlGlosaKyAc18ItemConcepto.Text = string.Empty;
            }
        }
        #endregion

        #region METODOS DESTINADOS AL MANEJO DE LAS RELACIONES
        void SetRelacion(string Nombre, DropDownList oDDL)
        {
            if (oDDL == null) return;
            for (int i = 0; i < Relaciones.Length; i++)
            {
                if (Relaciones[i].IdentityName == Nombre)
                {
                    oDDL.DataSource = Relaciones[i].DataSource;
                    oDDL.DataBind();
                }
            }
        }
        #endregion
    }
}
